
var app = getApp();
var checkData = [];
var textArray = [];  //兴趣文字
var indexArray = [];  //下标数组
var iconIndex = [];  //

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hobbyData: [
      "球类运动",  "看书", "学习","象棋", "游泳", "篮球", "爱运动", "网球", "性格外向", "画画",
      "远足旅游", "参加公益活动", "烹饪", "桌游", "投资理财", "斗图", "乒乓球", "数独", "辩论", "相声",
      "魔术", "哲学", "打麻将", "话剧", "吉他", "爱好旅游", "街舞",  "学习", "远足", "钓鱼", "潜水","唱歌",
    ],
    morHoby: ['看书','数独'] //默认选中
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.initHobbyList();
  },



  /**
   * 事件绑定，点击爱好列表按钮，选中取消
   */
  hobbyBtnClick: function (res) {
    var that = this;
    var texts = res.currentTarget.dataset.texts;      //作为参数，传递进来的图片地址
    var index = res.currentTarget.dataset.index;      //  作为参数，传递进来的下标
    if (checkData.indexOf(texts) == -1) {
      if (checkData.length >= 5) {
        app.GBshowModalTip("提示", "最多只能选择5个爱好");
      }
      else {
        checkData.push(texts);
        //设置当前为选中
        if (indexArray.indexOf(index) == -1) {
          indexArray.push(index); 
          iconIndex[index] = index;
          this.setData({
            iconIndex: iconIndex
          })
        }
      }
    }
    else {
      //取消选中当前
      for (var i = 0; i < checkData.length; i++) {
        if (checkData[i] == texts) {
          checkData.splice(i, 1);
        }
      }
      //设置当前为取消选中
      for (var i = 0; i < indexArray.length; i++) {
        if (indexArray[i] == index) {
          indexArray.splice(i, 1);
        }
      }
      //设为初始值-1,
      iconIndex[index] = -1;
      this.setData({
        iconIndex: iconIndex
      })
    }
  },

  /**
   * 渲染爱好已选列表，让用户已选的爱好为选中状态
   */
  initHobbyList: function () {
    var that = this;
    var mvalue = wx.getStorageSync('checkData');
    console.log(mvalue)
    if (mvalue != "") {
      for (var i = 0; i < mvalue.length; i++) {
        that.data.morHoby.push(mvalue[i]);
      }
    }
    for (var i = 0; i < that.data.morHoby.length; i++) {
      var indexs = that.data.hobbyData.indexOf(that.data.morHoby[i]);
      iconIndex[indexs] = indexs;
      checkData.push(that.data.morHoby[i]);
      this.setData({
        iconIndex: iconIndex
      })
    }
  },


  /**
   * 事件方法
   * 爱好修改，点击保存按钮
   */
  saveHobbybtnClick: function () {
    if (checkData.length == 0) {
      app.GBshowModalTip("提示", "请选择爱好");
    }
    else {
      wx.setStorageSync('checkData', checkData);
      console.log(checkData)
      console.log('拼接后的兴趣：'+checkData.join(','))
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    checkData = [];
    iconIndex = [];
    indexArray = [];
    this.initHobbyList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})